package gao.xiaolei.service;

import gao.xiaolei.copyDao.*;
import gao.xiaolei.copyEntity.*;
import gao.xiaolei.dao.*;
import gao.xiaolei.dto.*;
import gao.xiaolei.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import xiaolei.gao.git.IGit;
import gao.xiaolei.util.ServiceName;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service("TestCopyService")
public class TestCopyService {

    @Resource(name = "TestRequirementDao")
    private TestRequirementDao testRequirementDao;

    @Resource(name = "TestRequirementCopyDao")
    private TestRequirementCopyDao testRequirementCopyDao;

    @Resource(name = "TestRequirementCheckDao")
    private TestRequirementCheckDao testRequirementCheckDao;

    @Resource(name = "TestReqCheckCopyDao")
    private TestReqCheckCopyDao testRequirementCheckCopyDao;

    @Resource(name = "TestDesignDao")
    private TestDesignDao testDesignDao;

    @Resource(name = "TestStepDao")
    private TestStepDao testStepDao;

    @Resource(name = "TestRequirementDesignCoverDao")
    private TestRequirementDesignCoverDao testRequirementDesignCoverDao;

    @Resource(name = "TestDesignCheckDao")
    private TestDesignCheckDao testDesignCheckDao;

    @Resource(name = "TestDesignCopyDao")
    private TestDesignCopyDao testDesignCopyDao;

    @Resource(name = "TestStepCopyDao")
    private TestStepCopyDao testStepCopyDao;

    @Resource(name = "TestReqDesignCoverCopyDao")
    private TestReqDesignCoverCopyDao testReqDesignCoverCopyDao;

    @Resource(name = "TestDesignCheckCopyDao")
    private TestDesignCheckCopyDao testDesignCheckCopyDao;

    @Resource(name = "TestRunDao")
    private TestRunDao testRunDao;

    @Resource(name = "TestRunLogDao")
    private TestRunLogDao testRunLogDao;

    @Resource(name = "TestRunLogStepDao")
    private TestRunLogStepDao testRunLogStepDao;

    @Resource(name = "TestRunCheckDao")
    private TestRunCheckDao testRunCheckDao;

    @Resource(name = "TestRunCopyDao")
    private TestRunCopyDao testRunCopyDao;

    @Resource(name = "TestRunLogCopyDao")
    private TestRunLogCopyDao testRunLogCopyDao;

    @Resource(name = "TestRunLogStepCopyDao")
    private TestRunLogStepCopyDao testRunLogStepCopyDao;

    @Resource(name = "TestRunCheckCopyDao")
    private TestRunCheckCopyDao testRunCheckCopyDao;

    @Resource(name = "GitInterface")
    private IGit gitInterface;

    @Autowired
    private RestTemplate restTemplate;

    @Transactional
    public void copy(int type, String project) {
        int i, j, k, length, length2, length3;
        String commitId = commitGit(project, type);//给当前测试项目保存保存git快照，因为后面要获得commitId所以要放在第一步
        if (type == 0) {//如果是要复制测试需求的副本的话
            List<TestRequirement> isPassRootReq = testRequirementDao.findByProjectAndStatusAndParentId(project, 3, "root");//获取通过审核的最顶端测试需求
            List<String> trqId = new LinkedList<>();//用于储存所有要复制的测试需求Id
            for (i = 0, length = isPassRootReq.size(); i < length; i++) {//遍历搜集所有测试需求的Id
                recurrenceGetChildren(isPassRootReq.get(i).getId(), trqId);
            }
            List<TestRequirement> toCopyReq = testRequirementDao.findAllById(trqId);//查找出所有要复制的测试需求
            for (i = 0, length = toCopyReq.size(); i < length; i++) {//复制测试需求
                testRequirementCopyDao.save(new TestReqCopy(toCopyReq.get(i), commitId));
                List<TestRequirementCheck> reqCheck = testRequirementCheckDao.findByTargetId(toCopyReq.get(i).getId());
                for (j = 0, length2 = reqCheck.size(); j < length2; j++) {//将测试需求的审核记录也一起复制
                    testRequirementCheckCopyDao.save(new TestReqCheckCopy(reqCheck.get(j)));
                }
            }
        } else if (type == 1) {//如果是要复制测试计划的副本的话
            List<TestDesign> toCopyDesign = testDesignDao.findByProjectAndStatus(project, 3);//查找出所有通过审核的测试计划
            for (i = 0, length = toCopyDesign.size(); i < length; i++) {
                testDesignCopyDao.save(new TestDesignCopy(toCopyDesign.get(i)));//保存要复制的测试计划
                List<TestStep> toCopyStep = testStepDao.findByTestDesign(toCopyDesign.get(i).getId());
                for (j = 0, length2 = toCopyStep.size(); j < length2; j++) {//保存要复制的测试计划的测试步骤已经步骤文件
                    testStepCopyDao.save(new TestStepCopy(toCopyStep.get(j), commitId));
                }
                List<TestRequirementDesignCover> toCopyCover = testRequirementDesignCoverDao.findByTestDesignId(toCopyDesign.get(i).getId());
                for (j = 0, length2 = toCopyCover.size(); j < length2; j++) {//保存测试计划覆盖的测试需求
                    testReqDesignCoverCopyDao.save(new TestReqDesignCoverCopy(toCopyCover.get(j)));
                }
                List<TestDesignCheck> toCopyCheck = testDesignCheckDao.findByTargetId(toCopyDesign.get(i).getId());
                for (j = 0, length2 = toCopyCheck.size(); j < length2; j++) {//保存测试计划的审核记录
                    testDesignCheckCopyDao.save(new TestDesignCheckCopy(toCopyCheck.get(j)));
                }
            }
        } else if (type == 2) {//如果是复制测试执行的话
            List<TestRun> toCopyRun = testRunDao.findByProjectAndStatus(project, 3);//查找出所有通过审核的测试执行
            for (i = 0, length = toCopyRun.size(); i < length; i++) {
                testRunCopyDao.save(new TestRunCopy(toCopyRun.get(i)));//保存要复制的测试执行
                List<TestRunLog> toCopyLog = testRunLogDao.findByTestRunId(toCopyRun.get(i).getId());
                for (j = 0, length2 = toCopyLog.size(); j < length2; j++) {//保存要复制的测试执行记录
                    testRunLogCopyDao.save(new TestRunLogCopy(toCopyLog.get(j)));
                    List<TestRunLogStep> toCopyLogStep = testRunLogStepDao.findByTestRunLog(toCopyLog.get(j).getId());
                    for (k = 0, length3 = toCopyLogStep.size(); k < length3; k++) {//保存要复制的测试执行记录的步骤
                        testRunLogStepCopyDao.save(new TestRunLogStepCopy(toCopyLogStep.get(k), commitId));
                    }
                }
                List<TestRunCheck> toCopyCheck = testRunCheckDao.findByTargetId(toCopyRun.get(i).getId());
                for (j = 0, length2 = toCopyCheck.size(); j < length2; j++) {//保存待复制的测试执行的审核记录
                    testRunCheckCopyDao.save(new TestRunCheckCopy(toCopyCheck.get(j)));
                }
            }
        }
//        else {//如果是复制测试报告的话
//
//        }
    }

    private void recurrenceGetChildren(String parentId, List<String> collect) {
        if (testRequirementDao.getLeaf(parentId) != 1) {//如果不是叶子节点的话
            List<String> children = testRequirementDao.getChildrenId(parentId);
            for (int i = 0, length = children.size(); i < length; i++) {//就要把儿子都找出来继续遍历
                recurrenceGetChildren(children.get(i), collect);
            }
        }
        collect.add(parentId);
    }

    //提交某个项目仓库
    private String commitGit(String project, int type) {
        gitInterface.add(project, ".");
        String message = null;
        switch (type) {
            case 0:
                message = "保存测试需求的副本";
                break;
            case 1:
                message = "保存测试计划的副本";
                break;
            case 2:
                message = "保存测试执行的副本";
                break;
//            default:
//                message = "保存测试报告的副本";
//                break;
        }
        return gitInterface.commit(project, message);
    }

    //获取项目下面复制的测试需求、测试计划、测试执行文件夹和测试分析和测试BUG
    public List<TestTreeDto> getCopyFolder(String projectId) {
        List<TestTreeDto> folderDto = new ArrayList<>(5);
        String name = null;
        int childrenCount, leaf;
        for (int i = 0; i < 5; i++) {
            if (i == 0) {
                name = "测试需求";
                childrenCount = testRequirementCopyDao.countByProjectAndParentId(projectId, "root");
                leaf = 0;
            } else if (i == 1) {
                name = "测试设计";
                childrenCount = testDesignCopyDao.countByProjectAndParentId(projectId, "root");
                leaf = 0;
            } else if (i == 2) {
                name = "测试执行";
                childrenCount = testRunCopyDao.countByProjectAndParentId(projectId, "root");
                leaf = 0;
            } else if (i == 3) {
                name = "测试分析";
                childrenCount = 0;
                leaf = 1;
            } else {
                name = "测试BUG";
                childrenCount = 0;
                leaf = 1;
            }
            folderDto.add(new TestTreeDto(null, name, projectId, leaf, null, childrenCount, i));
        }
        return folderDto;

    }

    //获取项目下面复制的测试需求、测试计划、测试执行、文件夹下面的最顶端节点
    public List<TestTreeDto> getRootItemList(String project, int type, String parentId) {//因为最顶层的parentId前端是临时创建的,所以要拿儿子节点时要返回去
        List<Object[]> items = null;
        if (type == 0) {
            items = testRequirementCopyDao.findByProjectAndParentIdOrderByTitle(project, "root");
            return objToTreeDto(items, type, true, parentId);
        } else if (type == 1) {
            items = testDesignCopyDao.findByProjectAndParentIdOrderByTitle(project, "root");
            return objToTreeDto(items, type, true, parentId);
        } else if (type == 2) {
            items = testRunCopyDao.findByProjectAndParentIdOrderByTitle(project, "root");
            return objToTreeDto(items, type, true, parentId);
        } else
            return new ArrayList<TestTreeDto>(0);
    }

    //获取项目下面复制的测试需求、测试计划、测试执行文件夹下面的某个节点(除了最顶端节点)的子节点列表
    public List<TestTreeDto> geChildrenItemList(String id, int type) {
        List<Object[]> items = null;
        if (type == 0) {
            items = testRequirementCopyDao.getChildren(id);
            return objToTreeDto(items, type, false, null);
        } else if (type == 1) {
            items = testDesignCopyDao.getChildren(id);
            return objToTreeDto(items, type, false, null);
        } else if (type == 2) {
            items = testRunCopyDao.getChildren(id);
            return objToTreeDto(items, type, false, null);
        } else
            return new ArrayList<TestTreeDto>(0);
    }

    //获取复制的测试需求的详细信息
    @Transactional
    public TrqCopyOutDto getCopyTrqDetail(String id) {
        TestReqCopy testReqCopy = testRequirementCopyDao.findById(id).orElseThrow(() -> new IllegalArgumentException("该测试需求副本(".concat(id).concat(")").concat("不存在！")));
        String description = new String(gitInterface.readFileFromCommit(testReqCopy.getProject(), testReqCopy.getCommitId(), makePath(id)), StandardCharsets.UTF_8);
        String author = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(testReqCopy.getAuthor()), String.class);
        return new TrqCopyOutDto(testReqCopy.getId(), testReqCopy.getTitle(), author, testReqCopy.getAuthor(), null, testReqCopy.getCreateDate(), testReqCopy.getUpdateDate(), null, description, testReqCopy.getProject(), testReqCopy.getGrade());
    }

    //获取某个复制的测试需求的的所有审核记录,但只是返回id,isPass,checkMan,checkDate,isFinish
    @Transactional
    public List<TestCheckLogDto> getCopyTrqCheckLog(String id) {
        List<TestReqCheckCopy> checkLogs = objToTestRequirement5(testRequirementCheckCopyDao.findByTarget(id));
        List<TestCheckLogDto> dtos = new ArrayList<>(checkLogs.size());
        for (int i = 0, length = checkLogs.size(); i < length; i++) {
            String checkManName = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(checkLogs.get(i).getCheckMan()), String.class);
            dtos.add(new TestCheckLogDto(checkLogs.get(i).getId(), checkManName, checkLogs.get(i).getIsPass(), checkLogs.get(i).getIsFinish(), checkLogs.get(i).getCheckDate()));
        }
        return dtos;
    }

    //获取某个复制的测试需求的审核记录的批注
    public String getCopyTrqCheckLogAnnotate(String id) {
        return testRequirementCheckCopyDao.getAnnotate(id);
    }

    //获取复制的测试计划的详细信息
    @Transactional
    public TestDesignOutDto getCopyDesignDetail(String id) {
        TestDesignCopy testDesign = testDesignCopyDao.findById(id).orElseThrow(() -> new IllegalArgumentException("该测试计划副本(".concat(id).concat(")").concat("不存在！")));
        String author = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(testDesign.getAuthor()), String.class);
        if (testDesign.getLeaf() == 0)//如果是主题文件夹就只要获取描述信息和作者就好了
            return new TestDesignOutDto(testDesign.getTitle(), author, testDesign.getAuthor(), testDesign.getDescribe(), testDesign.getCreateDate(), testDesign.getUpdateDate(), testDesign.getGrade());
        //不然就要把测试步骤、关联的测试需求、审核记录给全部拿出来
        List<TestStepCopy> steps = testStepCopyDao.findAllByTestDesign(id);
        List<TestStep> stepDto = new LinkedList<>();
        for (int i = 0, length = steps.size(); i < length; i++) {//将测试步骤的文件内容获取出来
            byte[] textByte = gitInterface.readFileFromCommit(testDesign.getProject(), steps.get(i).getCommitId(), makePath(steps.get(i).getId()));
            stepDto.add(new TestStep(steps.get(i).getId(), steps.get(i).getTestDesign(), steps.get(i).getName(), steps.get(i).getFrontNode(), steps.get(i).getBackNode(), steps.get(i).getExpectedResult(), new String(textByte, StandardCharsets.UTF_8)));
        }
        List<TestReqDesignCoverCopy> covers = testReqDesignCoverCopyDao.findAllByTestDesignId(id);
        List<TestRequirementDesignCoverDto> coverDtos = new LinkedList<>();
        for (TestReqDesignCoverCopy c : covers) {
            TestReqCopy req = testRequirementCopyDao.findById(c.getTestRequirementId()).orElseThrow(() -> new IllegalArgumentException("该测试需求副本(".concat(id).concat(")").concat("不存在！")));
            String reqAuthor = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(req.getAuthor()), String.class);
            byte[] textByte = gitInterface.readFileFromCommit(req.getProject(), req.getCommitId(), makePath(req.getId()));
            coverDtos.add(new TestRequirementDesignCoverDto(req.getId(), getTrqPath(req.getId()).concat(req.getTitle()), reqAuthor, new String(textByte, StandardCharsets.UTF_8)));
        }
        return new TestDesignOutDto(testDesign.getId(), testDesign.getTitle(), testDesign.getLeaf(), author, testDesign.getAuthor(), testDesign.getDescribe(), null, testDesign.getPriority(), testDesign.getCreateDate(), testDesign.getUpdateDate(), stepDto, coverDtos, testDesign.getGrade());
    }

    //获取某个复制测试计划的的所有审核记录,但只是返回id,isPass,checkMan,checkDate,isFinish
    @Transactional
    public List<TestCheckLogDto> getCopyDesignCheckLog(String id) {
        List<TestDesignCheckCopy> checkLogs = objToTestDesign5(testDesignCheckCopyDao.findByTarget(id));
        List<TestCheckLogDto> dtos = new ArrayList<>(checkLogs.size());
        for (int i = 0, length = checkLogs.size(); i < length; i++) {
            String checkManName = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(checkLogs.get(i).getCheckMan()), String.class);
            dtos.add(new TestCheckLogDto(checkLogs.get(i).getId(), checkManName, checkLogs.get(i).getIsPass(), checkLogs.get(i).getIsFinish(), checkLogs.get(i).getCheckDate()));
        }
        return dtos;
    }

    //获取某个复制的测试计划的审核记录的批注
    public String getCopyDesignCheckLogAnnotate(String id) {
        return testDesignCheckCopyDao.getAnnotate(id);
    }

    //获取复制的测试执行的详细信息
    @Transactional
    public TestRunOutDto getCopyRunDetail(String id) {
        TestRunCopy testRun = testRunCopyDao.findById(id).orElseThrow(() -> new IllegalArgumentException("该测试执行副本(".concat(id).concat(")").concat("不存在！")));
        String author = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(testRun.getAuthor()), String.class);
        if (testRun.getLeaf() == 0)//如果是主题文件夹就只要获取描述信息和作者就好了
            return new TestRunOutDto(testRun.getTitle(), author, testRun.getAuthor(), testRun.getDescribe(), testRun.getCreateDate(), testRun.getUpdateDate(), testRun.getGrade());
        //不然就要把测试执行记录拿出来
        List<TestRunLogCopy> testRunLogsCopy = testRunLogCopyDao.findAllByTestRunIdOrderByCreateDateDesc(id);
        List<TestRunLogDto> testRunLogDtoList = new ArrayList<>(testRunLogsCopy.size());
        for (int i = 0, length = testRunLogsCopy.size(); i < length; i++) {//将测试执行记录获取出来封装到Dto中
            List<TestRunLogStepCopy> stepsCopy = testRunLogStepCopyDao.findByTestRunLog(testRunLogsCopy.get(i).getId());
            List<TestRunLogStep> steps = new LinkedList<>();
            for (int j = 0, length2 = stepsCopy.size(); j < length2; j++) {//将执行步骤都获取出来
                String stepText = new String(gitInterface.readFileFromCommit(testRun.getProject(), stepsCopy.get(j).getCommitId(), makePath(stepsCopy.get(j).getId())), StandardCharsets.UTF_8);
                String reasonText = new String(gitInterface.readFileFromCommit(testRun.getProject(), stepsCopy.get(j).getCommitId(), makePath(stepsCopy.get(j).getReasonId())), StandardCharsets.UTF_8);
                stepsCopy.get(j).setDescription(stepText);
                stepsCopy.get(j).setReason(reasonText);
                steps.add(new TestRunLogStep(stepsCopy.get(j).getId(), stepsCopy.get(j).getName(), stepsCopy.get(j).getFrontNode(), stepsCopy.get(j).getBackNode(), stepsCopy.get(j).getExpectedResult(), stepsCopy.get(j).getTestRunLog(), stepsCopy.get(j).getIsPass(), stepsCopy.get(j).getReasonId(), reasonText, stepText));
            }
            TestRunLog testRunLog = new TestRunLog(testRunLogsCopy.get(i));
            testRunLogDtoList.add(new TestRunLogDto(testRunLog, steps));
        }
        return new TestRunOutDto(testRun.getTitle(), author, testRun.getAuthor(), null, testRun.getCreateDate(), testRun.getUpdateDate(), testRun.getDescribe(), testRun.getPriority(), testRun.getTestDesign(), getTestDesignPath(testRun.getTestDesign()), testRunLogDtoList, testRun.getGrade());
    }

    //获取某个复制测试执行的所有审核记录,但只是返回id,isPass,checkMan,checkDate,isFinish
    @Transactional
    public List<TestCheckLogDto> getRunCopyCheckLog(String id) {
        List<TestRunCheckCopy> checkLogs = objToTestRun5(testRunCheckCopyDao.findByTarget(id));
        List<TestCheckLogDto> dtos = new ArrayList<>(checkLogs.size());
        for (int i = 0, length = checkLogs.size(); i < length; i++) {
            String checkManName = restTemplate.getForObject(ServiceName.AUTH_SERVICE.getServiceName().concat("/user/realName/").concat(checkLogs.get(i).getCheckMan()), String.class);
            dtos.add(new TestCheckLogDto(checkLogs.get(i).getId(), checkManName, checkLogs.get(i).getIsPass(), checkLogs.get(i).getIsFinish(), checkLogs.get(i).getCheckDate()));
        }
        return dtos;
    }

    //获取某个复制的测试运行的审核记录的批注
    public String getCopyRunCheckLogAnnotate(String id) {
        return testRunCheckCopyDao.getAnnotate(id);
    }


    //给复制的测试需求、计划、执行打分
    @Transactional
    public void gradeCopyItem(int type, String id, int grade, String judge) {
        if (type == 0)//如果是给测试需求打分
            testRequirementCopyDao.updateGrade(id, grade, judge);
        else if (type == 1)//如果是给测试计划打分
            testDesignCopyDao.updateGrade(id, grade, judge);
        else if (type == 2)//如果是给测试执行打分
            testRunCopyDao.updateGrade(id, grade, judge);
    }

    //根据项目Id将所有副本删除(只是删除数据库)
    @Transactional
    public void deleteAllCopyByProject(String projectId) {
        List<String> trqId = testRequirementCopyDao.findIdsByProject(projectId);//获取测试需求Id
        testRequirementCheckCopyDao.deleteAllByTargetIdIn(trqId);//删除测试需求的审核记录
        List<String> designId = testDesignCopyDao.findIdsByProject(projectId);//获取测试计划Id
        testStepCopyDao.deleteAllByTestDesignIn(designId);//删除测试计划下的步骤
        testReqDesignCoverCopyDao.deleteAllByTestDesignIdIn(designId);//删除测试计划下的关联测试需求
        testDesignCheckCopyDao.deleteAllByTargetIdIn(designId);//删除测试计划的审核记录
        List<String> runId = testRunCopyDao.findIdsByProject(projectId);//获取测试执行Id
        if (runId.size() > 0) {//把空集合放到in()会抛异常
            List<String> runLogIds = testRunLogCopyDao.findIdsByTestRunIdIn(runId);//获取测试执行记录Id
            testRunLogStepCopyDao.deleteAllByTestRunLogIn(runLogIds);//删除测试执行记录下的测试步骤
            testRunLogCopyDao.deleteAllByTestRunIdIn(runId);//删除测试执行记录
            testRunCheckCopyDao.deleteAllByTargetIdIn(runId);//获取测试执行的审核记录
        }
        testRequirementCopyDao.deleteAllByProject(projectId);//删除复制的测试需求
        testDesignCopyDao.deleteAllByProject(projectId);//删除复制的测试计划
        testRunCopyDao.deleteAllByProject(projectId);//删除复制的测试执行
    }

    //将查处来的节点信息转换为Dto
    private List<TestTreeDto> objToTreeDto(List<Object[]> items, int type, boolean isRoot, String parentId) {
        List<TestTreeDto> dtos = new ArrayList<>(items.size());
        int childrenCount;
        for (int i = 0, length = items.size(); i < length; i++) {
            childrenCount = 0;
            if ((int) items.get(i)[3] == 0) {
                if (type == 0)
                    childrenCount = testRequirementCopyDao.getChildrenCount((String) items.get(i)[0]);
                else if (type == 1)
                    childrenCount = testDesignCopyDao.getChildrenCount((String) items.get(i)[0]);
                else if (type == 2)
                    childrenCount = testRunCopyDao.getChildrenCount((String) items.get(i)[0]);
                else
                    childrenCount = 0;
            }
            dtos.add(new TestTreeDto((String) items.get(i)[0], (String) items.get(i)[1], isRoot ? parentId : (String) items.get(i)[2], (Integer) items.get(i)[3], isRoot ? (String) items.get(i)[4] : null, childrenCount, type));
        }
        return dtos;
    }

    //将uuid变为路径形式
    private String makePath(String uuid) {
        return uuid.substring(0, 2).concat("/").concat(uuid.substring(2, 32));
    }

    //用于将从数据库里查出来的List<Object[]>转换为List<TestRequirementCheck>(id,isPass,checkMan,isFinish,checkDate)
    private List<TestReqCheckCopy> objToTestRequirement5(List<Object[]> os) {
        List<TestReqCheckCopy> ts = new ArrayList<>(os.size());
        for (int i = 0, length = os.size(); i < length; i++) {
            ts.add(new TestReqCheckCopy((String) os.get(i)[0], (Integer) os.get(i)[1], (String) os.get(i)[2], (Integer) os.get(i)[3], (LocalDateTime) os.get(i)[4]));
        }
        return ts;
    }

    //用于将从数据库里查出来的List<Object[]>转换为List<TestDesignCheck>(id,isPass,checkMan,isFinish,checkDate)
    private List<TestDesignCheckCopy> objToTestDesign5(List<Object[]> os) {
        List<TestDesignCheckCopy> ts = new ArrayList<>(os.size());
        for (int i = 0, length = os.size(); i < length; i++) {
            ts.add(new TestDesignCheckCopy((String) os.get(i)[0], (Integer) os.get(i)[1], (String) os.get(i)[2], (Integer) os.get(i)[3], (LocalDateTime) os.get(i)[4]));
        }
        return ts;
    }

    //用于将从数据库里查出来的List<Object[]>转换为List<testRunCheck>(id,isPass,checkMan,isFinish,checkDate)
    private List<TestRunCheckCopy> objToTestRun5(List<Object[]> os) {
        List<TestRunCheckCopy> ts = new ArrayList<>(os.size());
        for (int i = 0, length = os.size(); i < length; i++) {
            ts.add(new TestRunCheckCopy((String) os.get(i)[0], (Integer) os.get(i)[1], (String) os.get(i)[2], (Integer) os.get(i)[3], (LocalDateTime) os.get(i)[4]));
        }
        return ts;
    }

    //获取某一个复制测试需求的路径（但是不包括本身）
    private String getTrqPath(String trqId) {
        List<String> path = new LinkedList<>();
        String parentId = testRequirementCopyDao.getParentId(trqId);
        while (!"root".equals(parentId) && parentId != null) {
            path.add(testRequirementCopyDao.getTitle(parentId));
            parentId = testRequirementCopyDao.getParentId(parentId);
        }
        StringBuilder result = new StringBuilder();
        for (int i = path.size() - 1; i >= 0; i--) {
            result.append(path.get(i)).append("/");
        }
        return result.toString();
    }

    //获取某一个测试计划的路径
    private String getTestDesignPath(String id) {
        List<String> path = new LinkedList<>();
        String parentId = testDesignCopyDao.getParentId(id);
        while (!"root".equals(parentId) && parentId != null) {
            path.add(testDesignCopyDao.getTitle(parentId));
            parentId = testDesignCopyDao.getParentId(parentId);
        }
        StringBuilder result = new StringBuilder();
        for (int i = path.size() - 1; i >= 0; i--) {
            result.append(path.get(i)).append("/");
        }
        result.append(testDesignCopyDao.getTitle(id));
        return result.toString();
    }
}
